package com.taobao.ic.scan.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taobao.common.dao.persistence.SqlMapBaseDAO;
import com.taobao.common.dao.persistence.exception.DAOException;
import com.taobao.ic.scan.dao.SpuDAO;
import com.taobao.ic.scan.route.DBRoutingFactory;
import com.taobao.item.domain.spu.SpuDO;

public class IbatisSpuDAO extends SqlMapBaseDAO implements SpuDAO {

	public SpuDO getSpuFromMysql(long spuId) throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("spuId", spuId);
		return (SpuDO) this.executeQueryForObject(
				"MysqlIbatisSpuDAO.getSpuById", params,
				DBRoutingFactory.getMysqlspu());
	}

	public SpuDO getSpuFromOracle(long spuId) throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("spuId", spuId);
		return (SpuDO) this.executeQueryForObject(
				"OracleIbatisSpuDAO.getSpuById", params,
				DBRoutingFactory.getOracleheart());
	}

	public long getMinSpuIdFromOracle() throws DAOException {
		// TODO Auto-generated method stub
		Map<String, Object> params = new HashMap<String, Object>();
		return (Long) this.executeQueryForObject(
				"OracleIbatisSpuDao.findMinSpuId", params,
				DBRoutingFactory.getOracleheart());
	}

	public long getMaxSpuIdFromOracle() throws DAOException {
		// TODO Auto-generated method stub
		Map<String, Object> params = new HashMap<String, Object>();
		return (Long) this.executeQueryForObject(
				"OracleIbatisSpuDao.findCurMaxSpuId", params,
				DBRoutingFactory.getOracleheart());
	}

	@SuppressWarnings("unchecked")
	public List<SpuDO> getSpusByLimitRange(long startId, long endId)
			throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startId", startId);
		params.put("endId", endId);
		return (List<SpuDO>) this.executeQueryForList(
				"OracleIbatisSpuDAO.getSimpleSpus", params,
				DBRoutingFactory.getOracleheart());
	}

	@SuppressWarnings("unchecked")
	public List<SpuDO> getSpusByLimitRange(Date startTime, Date endTime)
			throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startDate", startTime);
		params.put("endDate", endTime);
		return (List<SpuDO>) this.executeQueryForList(
				"OracleIbatisSpuDAO.getSimpleSpusByLimitDate", params,
				DBRoutingFactory.getOracleheart());
	}

	public void insertSpuToMysql(SpuDO spu) throws DAOException {
		executeInsert("MysqlIbatisSpuDAO.insertSpu", spu,
				DBRoutingFactory.getMysqlspu());
	}

	public void insertSpuDuplicateCheckToMysql(SpuDO spu) throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryId", spu.getCategoryId());
		params.put("properties", spu.getKeyProperties());
		executeInsert("SpuDuplicateCheckDao.insertCheck", params,
				DBRoutingFactory.getMysqlspu());
	}

}
